package 查找算法.插值查找;

public class InterpolationSearch {
    public static void main(String[] args) {
        int[] arr = new int[100000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }
        System.out.println(interpolationSearch(arr,0,arr.length-1,8888));
    }

    private static int interpolationSearch(int[] array, int l, int r, int key) {
        //计算离查找值最近的下标
        int m = l + (int)(1.0 * (key - array[l]) / (array[r] - array[l]) * (r - l));
        if (l > r){
            return -1;
        }
        if (array[m] == key) {
            return m;
        }else if (array[m] < key){
            return interpolationSearch(array,m + 1,r,key);
        }else {
            return interpolationSearch(array,l,m - 1, key);
        }
    }
}
